<?php

if (!defined('IN_XLP')) {
    exit('Access Denied');
}

/**
 * Description of menuAction
 * 银行管理
 * @author skyinter
 */
class bankAction extends commonAction {

    function __construct() {
        parent::__construct();
        parent::_checkLogin();
    }

    

    /**
     * 银行列表
     */
    public function index() {
        $page = $this->_get('page') ? $this->_get('page') : '1';
        $keyword = $this->_get('keyword') ? $this->_get('keyword') : '';
        $list = D('bank')->page($page , 20)->order('id DESC')->findAll();
        $total = M('bank')->getTotal();

        $this->assign('list', $list);
        $this->assign('total', $total);
        $this->assign('page', $page);
        $this->assign('keyword', $keyword);
        $this->display();
    }

    public function add() {
        if ($_POST) {
            $data['name'] = $this->_post('name');
            $data['sort'] = $this->_post('sort');
            $data['is_show'] = $this->_post('is_show');

            $result = D('bank')->insert($data);
            if ($result > 0) {
                //缓存数据
                $banks = M('bank')->field("id,name")->where(array('is_show'=>1))->order("sort DESC")->findAll();
                if(!empty($banks)) {
                    F('bank/bank_list' , $banks);
                }
                $this->JsonReturn('添加成功', null, 1);
            } else {
                $this->JsonReturn('添加失败', null, 0);
            }
            exit;
        }
        
        $this->display();
    }




    public function ajax() {
        $id = $this->_get('id') ? $this->_get('id') : null;
        $type = $this->_get('type') ? $this->_get('type') : null;

        switch ($type) {
            case 'down': //商品下架
                $data = array('state' => 2);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'del': //商品删除
                $data = array('is_del' => 1);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'up': //商品上架
                $data = array('state' => 1);
                $stat = D('goods')->modify($id, $data);
                break;
            case 'bankSave': //商品类别名称更改
                $data = array('name' => $this->_get('name'),'is_show' => $this->_get('is_show',0), 'sort' => $this->_get('sort'));
                $stat = M('bank')->update($data , array('id' => $id));
                break;
            case 'cateDel': //分类
                $data = array('is_del' => 1);
                $stat = D('goodsCategory')->modify($id, $data);
                break;
            default:
                $stat = 0;
                break;
        }

        if ($stat > 0) {
            $info = '请求成功';
            //缓存数据
            $banks = M('bank')->field("id,name")->where(array('is_show'=>1))->order("sort DESC")->findAll();
            if(!empty($banks)) {
                F('bank/bank_list' , $banks);
            }
            $this->JsonReturn($info, null, 1);
        } else {
            $info = '请求失败';
            $this->JsonReturn($info, null, 0);
        }
    }

}
